#define  _SYSTEM_GLOBAL_
#include "config.h"


void returnSystemInit(void)
{
	BYTE buff[1] = {0x01};

	//systemDisplayMsg(0x01,buff,sizeof(buff));
}

void controlSendTouchSrcDataEnable(BOOL bOn)
{
	BYTE buff[1] = {0x00};
	if (bOn)
	{
		buff[0] = 0x01;
	}

	systemDisplayMsg(0x02,buff,sizeof(buff));
}

void controlSendTouchSrcDataCali(U16 x1,U16 y1,U16 x2,U16 y2)
{
	BYTE buff[8];
	U16 temp;
	if (x1 > x2)
	{
		temp = x1;
		x1 = x2;
		x2 = temp;
	}
	if (y1 > y2)
	{
		temp = y1;
		y1 = y2;
		y2 = temp;
	}

	buff[0] = x1;buff[1] = x1 >> 8;
	buff[2] = y1;buff[3] = y1 >> 8;
	buff[4] = x2;buff[5] = x2 >> 8;
	buff[6] = y2;buff[7] = y2 >> 8;

	systemDisplayMsg(0x03,buff,sizeof(buff));
}

const U8 screenBrightLevelNight[3]={5,25,50};
const U8 screenBrightLevelDay[3]={50,60,100};

void controlLightBright(U8 iLight)
{
	BYTE buff[1];
	buff[0] = iLight;

	systemDisplayMsg(0x05,buff,sizeof(buff));
}

void systemFreeIdleProc(void)
{
	if (canInfo.CarLightStatus)
	{
		BaseDATA.dayNight = TRUE;
		systemInfo.prePanelPWM = canInfo.CarLightStatus*10;
	}
	else if (IOInfo.innerIOLight)
	{
		BaseDATA.dayNight = TRUE;
		systemInfo.prePanelPWM = 100;
	}
	else
	{
		BaseDATA.dayNight = FALSE;
		systemInfo.prePanelPWM = 0;
	}
	
	if (eLightAuto == BaseDATA.restoreData.settingDayNightAuto)
	{
		if (eLightDay == BaseDATA.dayNight)
		{
			systemInfo.preLightBright = screenBrightLevelDay[BaseDATA.restoreData.settingLight];
		}
		else
		{
			systemInfo.preLightBright = screenBrightLevelNight[BaseDATA.restoreData.settingLight];
		}

		systemInfo.tmpPanelPWM = systemInfo.prePanelPWM;
	}
	else if (eLightDay == BaseDATA.restoreData.settingDayNightAuto)
	{
		systemInfo.preLightBright = screenBrightLevelDay[BaseDATA.restoreData.settingLight];
		systemInfo.tmpPanelPWM = 0;
	}
	else if (eLightNight == BaseDATA.restoreData.settingDayNightAuto)
	{
		systemInfo.preLightBright = screenBrightLevelNight[BaseDATA.restoreData.settingLight];
		if (systemInfo.prePanelPWM)
		{
			systemInfo.tmpPanelPWM = systemInfo.prePanelPWM;
		}
		else
		{
			systemInfo.tmpPanelPWM = 100;
		}
	}

	if (!systemInfo.bRecSystemInit)
	{
		systemInfo.preLightBright = 0;
	}
	else if (disBackOnly == BaseDATA.disPageWhatTo || disBackVideo == BaseDATA.disPageWhatTo)
	{
	}
	else if (BaseDATA.innerStatus.innerWorkStatus < eWorkWork)
	{
		systemInfo.preLightBright = 0;
	}
	
	if (systemInfo.curLightBright != systemInfo.preLightBright)
	{
		systemInfo.curLightBright = systemInfo.preLightBright;
		//controlLightBright(systemInfo.curLightBright);
	}

	if (systemInfo.curPanelPWM != systemInfo.tmpPanelPWM)
	{
		systemInfo.curPanelPWM = systemInfo.tmpPanelPWM;

		//PWM_Ratio(PANEL_PWM_CHANNEL,systemInfo.curPanelPWM);

//		ipcStartEvent(EVENT_GLOBAL_NAVI_BOARD_NAVI_NIGHT,systemInfo.curPanelPWM,NULL,NULL);
	}
}


void ipcEventProcSystem(ULONG enumWhatEvent,ULONG lPara,BYTE *p,ULONG length)
{
	BOOL bNeedClear = TRUE;

	switch (enumWhatEvent)
	{
		case EVENT_GLOBAL_AUTO_CLR_NAVI_BOARD_INIT:
			//ipcStartEvent(EVENT_GLOBAL_NAVI_BOARD_NAVI_NIGHT,systemInfo.curPanelPWM,NULL,NULL);
			break;

		case EVENT_GLOBAL_SYSTEM_SEND_TOUCH_CALI_DATA:
			controlSendTouchSrcDataCali(
				BaseDATA.restoreData.touchPanelX1
				,BaseDATA.restoreData.touchPanelY1
				,BaseDATA.restoreData.touchPanelX2
				,BaseDATA.restoreData.touchPanelY2);
			break;

		case EVENT_GLOBAL_SYSTEM_FACTORY:
			memset(&BaseDATA.restoreData,0,sizeof(RESTORE_DATA));
			storageWrite();
			GoToSystem(0);
			break;
			
		default:
			bNeedClear = FALSE;
			break;
	}

	if (bNeedClear)
	{
		ipcClearEvent(enumWhatEvent);
	}
}

void systemInit(void)
{
	memset(&systemInfo,0,sizeof(SYSTEM_INFO));

	systemInfo.preLightBright = 100;
	systemInfo.prePanelPWM = 0;
	//PWM_Ratio(PANEL_PWM_CHANNEL,systemInfo.curPanelPWM);
	
	//DEBUG
	systemInfo.pageCDActive = TRUE;
}

